from common.base import Base, By


# 定义ShopCartPage类，继承Base
class ShopCartPage(Base):

    # 购物车增加操作：
    def add_shopcart(self):
        # 定位一个商品
        self.click((By.XPATH, '/html/body/div[6]/div/div/div[2]/div[2]/div/a[7]/div[3]'))

        # 定位立即购买，加入商品到购物车
        self.click((By.XPATH, '//*[@id="ECS_FORMBUY"]/ul/li[7]/table/tbody/tr/td[1]/a/img'))

    def continue_shop(self):
        # 定位继续购物按钮点击
        self.click((By.XPATH, '/html/body/div[6]/div[1]/table/tbody/tr/td[1]/a/img'))

    # 购物车查看操作
    def check_shopcart(self):
        # 定位购物车按钮点击，查看购物车
        self.click((By.XPATH, '//*[@id="ECS_CARTINFO"]/a'))

    # 购物车修改操作
    def modify_shopcart(self, num=3):
        # 定位购物车商品购买数量清空并修改
        self.find_element((By.CLASS_NAME, 'inputBg')).clear()
        self.find_element((By.CLASS_NAME, 'inputBg')).send_keys(num)

        # 定位更新购物车按钮点击
        self.click((By.XPATH, '//*[@id="formCart"]/table[2]/tbody/tr/td[2]/input[2]'))

    # 购物车删除操作
    def delete_goods(self):
        # 定位购物车删除按钮点击
        self.click((By.LINK_TEXT, '删除'))

        # 切换到弹窗点击确认
        alert = self.driver.switch_to.alert
        alert.accept()

    def clear_shopcart(self):
        # 定位清空购物车按钮点击
        self.click((By.XPATH, '//*[@id="formCart"]/table[2]/tbody/tr/td[2]/input[1]'))
